package com.tool.btn;

import com.tool.role.RoleData;
import com.tool.role.RoleLingFa;
import com.tool.tcpimg.UIUtils;
import come.tool.JDialog.TiShiUtil;
import org.apache.commons.lang.StringUtils;
import org.come.Frame.*;
import org.come.bean.Skill;
import org.come.entity.Goodstable;
import org.come.entity.RoleSummoning;
import org.come.model.Lingbao;
import org.come.until.FormsManagement;
import org.come.until.GoodsListFromServerUntil;
import org.come.until.UserMessUntil;
import org.come.until.Util;

import java.awt.event.MouseEvent;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class LingBaoZhiYuanBtn extends MoBanBtn {
    public LingBaoZhiYuanBtn(String iconpath, int type, String text) {
        super(iconpath, type, UIUtils.COLOR_BTNTEXT);
        this.setText(text);
        this.setFont(UIUtils.TEXT_FONT);
        this.setVerticalTextPosition(0);
        this.setHorizontalTextPosition(0);
    }

    public void chooseyes() {
    }

    public void chooseno() {
    }

    public void nochoose(MouseEvent e) {
        if (this.getText().equals("支援列表")) {
            RoleData roleData = RoleData.getRoleData();
            String helpLing = roleData.getPackRecord().getHelpLing();
            if(StringUtils.isBlank(helpLing)){
                Lingbao[] lingBaos = RoleLingFa.getRoleLingFa().getLingBaos();
                List<Lingbao> s = new ArrayList<>();
                for (Lingbao lingBao : lingBaos) {
                    if(lingBao != null)
                        s.add(lingBao);
                }
                if(s.size()>0) {
                    BigDecimal[] array = s.stream().map(Lingbao::getBaoid).toArray(BigDecimal[]::new);
                    String join = StringUtils.join(array, "|");
                    RoleData.getRoleData().sendHelpLingbao(join);
                }
            }

            String helpLing1 = RoleData.getRoleData().getPackRecord().getHelpLing();
            List<Lingbao> lingbaoList = new ArrayList<>();
            List<String> delList = new ArrayList<>();
            if(StringUtils.isNotEmpty(helpLing1)){
                //没有灵宝id追加到后面
                for (Lingbao lingBao : RoleLingFa.getRoleLingFa().getLingBaos()) {
                    if(lingBao == null)
                        continue;
                    if(!helpLing1.contains(lingBao.getBaoid().toString())){
                        helpLing1+="|"+lingBao.getBaoid().toString();
                    }
                }
                String[] split = helpLing1.split("\\|");
                for (String s : split) {
                    Boolean b = true;
                    try {
                        for (Lingbao lingBao : RoleLingFa.getRoleLingFa().getLingBaos()) {
                            if (lingBao == null)
                                continue;
                            if (lingBao.getBaoid().toString().equals(s)) {
                                lingbaoList.add(lingBao);
                                b = false;
                                break;
                            }
                        }

                    }catch (Exception e1){
                        continue;
                    }
                    if(b)
                        delList.add(s);
               }
                List<String> collect = Arrays.asList(split);
                List<String> collect1 =new ArrayList<>();
                for (String s : collect) {
                    Boolean b = true;
                    for (String string : delList) {
                        b =false;
                        break;
                    }
                    if(b)
                        collect1.add(s);
                }

                if(collect1.size()>0) {
                    lingbaoList.clear();
                    String[] lingIds = new String[collect1.size()];
                    collect1.toArray(lingIds);
                    String join = StringUtils.join(lingIds, "|");
                    RoleData.getRoleData().sendHelpLingbao(join);
                    for (String s : lingIds) {
                        for (Lingbao lingBao : RoleLingFa.getRoleLingFa().getLingBaos()) {
                            if (lingBao == null)
                                continue;
                            if (lingBao.getBaoid().toString().equals(s.toString())) {
                                lingbaoList.add(lingBao);
                                break;
                            }
                        }
                    }

                    }
                LingHelpListJframe.getLingHelpListJframe().getLingHelpListJpanel().init(lingbaoList);
            }else{
                LingHelpListJframe.getLingHelpListJframe().getLingHelpListJpanel().init(lingbaoList);
            }
            FormsManagement.showForm(622);
        }
    }
}
